﻿@using Newtouch.Common.Operator
@model dynamic

@{
    ViewBag.Title = "证照维护";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    .dv-searchP {
        float: left;
        margin-left: 10px;
    }

        .dv-searchP div {
            float: left;
            margin-right: 20px;
            width: 130px;
        }

        .dv-searchP select {
            padding: 2px 4px;
            line-height: 24px;
            height: 22px;
            display: initial;
        }

    .sp-operate {
        margin-left: 5px;
        color: #00a0ea;
    }
</style>
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="fa fa-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>新建</a>
        </div>
        <div class="btn-group">
            <a class="btn btn-primary" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改</a>
        </div>
        <div class="btn-group">
            <a class="btn btn-primary" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除</a>
        </div>
    </div>
    <div class="dv-searchP">
        <div>
            <select class="form-control" id="belongedId" onchange="changeBelonged()">
                <option value="">==选择所属==</option>
            </select>
        </div>
        <div>
            <select class="form-control" id="licenceTypeId">
                <option value="">==选择类型==</option>
            </select>
        </div>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="证照号/对象名称" style="width: 180px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>
<script lang="javascript">
    $(function () {
        gridList();
        Init();
    });

    //初始化
    function Init() {
        //加载所属
        $("#belongedId").bindSelect({
            url: "/LicenceManage/Licence/Getbelonged"
        });
        $("#licenceTypeId").bindSelect({
            url: "/LicenceManage/Licence/GetlicenceType"
        });
    }

    //切换所属 获取类型
    function changeBelonged() {
        var s = $("#belongedId").val();
        if (s !== "") {
            //加载类型
            $("#licenceTypeId").bindSelect({
                url: "/LicenceManage/Licence/GetlicenceType?belonged=" + $("#belongedId").val()
            });
        }
    }

    //get data
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/LicenceManage/Licence/GetLicenceGridJson",
            postData:getPostData(),
            height: $(window).height() - 120,
            colModel: [
                { label: "主键", name: "Id", hidden: true, key: true },
                { label: "证照所属", name: "belonged", width: 100, align: "left" },
                { label: "对象名称", name: "objectName", width: 100, align: "left" },
                { label: "证照类型", name: "typeName", width: 130, align: "left" },
                { label: '证照号', name: 'licenceNo', width: 100, align: 'left' },
                { label: '起效日期', name: 'qxrq', width: 70, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
                { label: '失效日期', name: 'sxrq', width: 70, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
                { label: '文件地址', name: 'fileUrl', width: 120, align: 'left' },
                {
                    label: "有效", name: "zt", width: 50, align: "center",
                    formatter: function (cellvalue) { return cellvalue === "1" ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>"; }
                },
                { label: '创建人', name: 'CreatorCode', width: 70, align: 'left' },
                { label: '创建时间', name: 'CreateTime', width: 110, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' } },
                {
                    label: '操作', name: 'objectName', width: 50, align: 'center',
                    formatter: function (cellvalue) {
                        return '<span class="sp-operate" onclick="downloadFile()">下载</span>' }
                },
                { label: '最后修改', name: 'LastModifierCode', hidden: true },
                { label: '最后修改时间', name: 'LastModifyTime', hidden: true  }
            ],
            pager: "#gridPager",
            sortname: 'CreateTime desc',
            viewrecords: true
        });
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam',
            {
                postData: getPostData()
            }).trigger('reloadGrid');
        });
    }

    //组装请求参数
    function getPostData() {
        return {
            keyWord: $("#txt_keyword").val(),
            belongedId: $("#belongedId").val(),
            licenceTypeId: $("#licenceTypeId").val()
        };
    }

    //回车触发查询事件
    $("#txt_keyword").keydownEnterEvent(function() {
        $("#btn_search").trigger("click");
    });

    //新增
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增",
            url: "/LicenceManage/Licence/Form",
            width: "550px",
            height: "310px",
            callBack: function (iframeId) {
                $.currentWindow(iframeId).AcceptClick(function() {
                    cleanSearchInput();
                    $('#gridList').jqGrid("clearGridData"); //清除明细的grid
                    $("#gridList").resetSelection();
                    $("#gridList").trigger("reloadGrid");
                });
            }
        });
    }

    //修改
    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().Id;
        if (!!!keyValue) {
            $.modalAlert("请选中一条信息", 'warning');
            return;
        }
        @*var thisOrgId = $("#gridList").jqGridRowValue().OrganizeId;
        if (!!!thisOrgId && '@(!string.IsNullOrEmpty(OperatorProvider.GetCurrent().OrganizeId) && OperatorProvider.GetCurrent().OrganizeId != OperatorProvider.GetCurrent().TopOrganizeId)' === 'True') {
            $.modalAlert("无权限修改", 'warning');
            return;
        }*@
        $.modalOpen({
            id: "Form",
            title: "修改",
            url: "/LicenceManage/Licence/Form?keyValue=" + keyValue,
            width: "550px",
            height: "310px",
            callBack: function (iframeId) {
                $.currentWindow(iframeId).AcceptClick(function() {
                    cleanSearchInput();
                    $('#gridList').jqGrid("clearGridData"); //清除明细的grid
                    $("#gridList").resetSelection();
                    $("#gridList").trigger("reloadGrid");
                });
            }
        });
    }

    //删除
    function btn_delete() {
        var keyValue = $("#gridList").jqGridRowValue().Id;
        if (!!!keyValue) {
            $.modalAlert("请选中一条信息", 'warning');
            return;
        }
        @*var thisOrgId = $("#gridList").jqGridRowValue().OrganizeId;
        if (!!!thisOrgId && '@(!string.IsNullOrEmpty(OperatorProvider.GetCurrent().OrganizeId) && OperatorProvider.GetCurrent().OrganizeId != OperatorProvider.GetCurrent().TopOrganizeId)' === 'True') {
            $.modalAlert("无权限修改", 'warning');
            return;
        }*@
        $.deleteForm({
            url: "/LicenceManage/Licence/DeleteLicForm",
            param: { keyValue: keyValue },
            success: function () {
                cleanSearchInput();
                $('#gridList').jqGrid("clearGridData"); //清除明细的grid
                $("#gridList").resetSelection();
                $("#gridList").trigger("reloadGrid");
            }
        });
        cleanSearchInput();
    }

    //sweep search marks
    function cleanSearchInput() {
        $("#txt_keyword").val("");
    }

    //下载
    function downloadFile() {
        var keyValue = $("#gridList").jqGridRowValue().Id;
        GetFileUrl(keyValue);
    }

    //获取当前文件url地址
    function GetFileUrl(t) {
        $.najax({
            url: "/LicenceManage/Licence/GetFileUrl?t=" + t,
            dataType: "text",
            type: "Get",
            async: false,
            success: function (res) {
                if (res === "") {
                    return "";
                }
                var o = JSON.parse(res);
                if (o != null) {
                    var a = $("<a href='http://" + o.Authority + o.uri+"' target='_blank'>baidu</a>").get(0);
                    var e = document.createEvent('MouseEvents');
                    e.initEvent( 'click', true, true );
                    a.dispatchEvent(e);
                }
                return "";
            }
        });
    }
</script>
